/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package abricots.entity.simulator;

import abricots.entity.ServerEntity;
import abricots.command.PlayerCommand;
import abricots.command.Command;
import abricots.entity.state.BasicEntityState;
import abricots.entity.state.EntityState;
import abricots.entity.state.ServerEntityState;
import java.util.List;

/**
 *
 * @author charly
 */
public class ServerEntitySimulator extends BasicEntitySimulator {
   public final static float SPEED = 0.04f;

   public ServerEntitySimulator(ServerEntity entity) {
      super(entity);
   }

   @Override
   public EntityState simulate(EntityState state, long targetGameTime, List<Command> commands) {
      ServerEntityState finalState = (ServerEntityState) super.simulate(state, targetGameTime, commands);
      if (finalState.isCollision()) {
         finalState.setCollision(false);
      }
      return finalState;
   }

   @Override
   protected BasicEntityState applyMovement(BasicEntityState state, int delta) {
      ServerEntityState ses = (ServerEntityState) state;
      if (ses.isCollision()) {
         return ses;
      }
      float toMove = state.getSpeed() * delta;
      float posX = state.getPosX();
      if (toMove > 0 && posX + toMove > 600) {
         float toMoveRight = 600 - posX;
         float toMoveLeft = -toMove + toMoveRight; //<0
         state.setPosX(posX + toMoveRight + toMoveLeft);
         state.setSpeed(-SPEED);
         return state;
      } else if (toMove < 0 && posX + toMove < 200) {
         float toMoveLeft = 200 - posX; //<0
         float toMoveRight = -toMove + toMoveLeft;
         state.setPosX(posX + toMoveRight + toMoveLeft);
         state.setSpeed(SPEED);
         return state;
      }
      state.setPosX(state.getSpeed() * delta + state.getPosX());
      return state;
   }

   @Override
   protected BasicEntityState applyCommand(BasicEntityState state, PlayerCommand command) {
      return state;
   }
}
